import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/svg.dart';

import 'package:get/get.dart';
import 'package:msh_checkbox/msh_checkbox.dart';
import 'package:smart_translator/app/event/DataCallBack.dart';
import 'package:smart_translator/app/nets/NetWorkManager.dart';
import 'package:smart_translator/app/utils/CommonUtil.dart';
import 'package:smart_translator/app/widgets/AppHeader.dart';

import '../../../routes/app_pages.dart';
import '../../../style/app_colors.dart';
import '../../../style/widget_style.dart';
import '../../../utils/tips.dart';
import '../controllers/regist_controller.dart';

class RegistView extends GetView<RegistController> {
  RegistView({super.key});

  final GlobalKey _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: SingleChildScrollView(
          controller: controller.scrollController.value,
          child: Stack(
            children: [
              Container(
                child: SvgPicture.asset(
                  "assets/images/header_part_bg.svg",
                  width: Get.width,
                ),
              ),
              AppHeader.obtainHeaderWithoutTitleAndRightPart(),
              Container(
                padding: EdgeInsets.all(15.w),
                width: Get.width,
                margin: EdgeInsets.only(top: 145.h),
                child: Column(
                  children: [
                     Row(
                       children: [
                         Text("账号注册",style: AppStyleThemeData.BIG_TITLE)
                       ],
                     ),
                     SizedBox(height: 15.h,),
                     Obx(()=> Container(
                       child: Form(
                         key: _formKey,
                         child: Column(
                           children: [
                             Container(
                                 child: TextFormField(
                                     decoration: InputDecoration(
                                         labelText: '请输入手机号/邮箱',
                                         labelStyle: AppStyleThemeData.LABEL_TEXT_STYLE,
                                         suffixIcon: controller.suffixIconsVisible.value ? Transform.scale(
                                           scale: 0.5, // 调整缩放比例
                                           child: InkWell(
                                             onTap: () {
                                               /*  phoneFocusNode.requestFocus();
                                      validateFocusNode.unfocus();*/
                                               controller.accountController.value.text = '';
                                               controller.setSuffixIconsVisible(false);
                                             },
                                             child: Image.asset('assets/images/ic_clear.webp'),
                                           ),
                                         ): SizedBox(),
                                         // 关键配置：自定义边框样式
                                         border: const UnderlineInputBorder(
                                           borderSide: BorderSide(color: AppColors.input_bottom_line_color), // 默认状态边框颜色
                                         ),
                                         enabledBorder: const UnderlineInputBorder(
                                           borderSide: BorderSide(color: AppColors.input_bottom_line_color), // 默认状态边框颜色
                                         ),
                                         focusedBorder: const UnderlineInputBorder(
                                           borderSide: BorderSide(color: Colors.blue), // 聚焦状态边框颜色
                                         ),
                                         errorBorder: const UnderlineInputBorder(
                                           borderSide: BorderSide(color: Colors.red), // 错误状态边框颜色
                                         ),
                                         focusedErrorBorder: const UnderlineInputBorder(
                                           borderSide: BorderSide(color: Colors.red), // 聚焦+错误状态边框颜色
                                         ),
                                         contentPadding: const EdgeInsets.fromLTRB(16, 20, 16, 14),
                                         // 可选：添加浮动标签动画间距调整
                                         floatingLabelAlignment: FloatingLabelAlignment.start

                                     ),
                                     controller: controller.accountController.value,
                                     onChanged: (v) {
                                       controller.onTextFieldChange(v);
                                     },
                                     validator: (v) {
                                       if(!CommonUtil.isEmail(v!) &&  !CommonUtil.isChinaPhoneLegal(v!)) {
                                         return '请输入正确的手机号或邮箱';
                                       }
                                     }
                                 )
                             ),
                             SizedBox(height: 10.h),
                             Container(
                                 child: TextFormField(
                                   keyboardType: TextInputType.text,
                                   controller: controller.passwordCodeController.value,
                                   obscureText: !controller.pwVisible.value, //This will obscure text dynamically
                                   decoration: InputDecoration(
                                     //labelText: label,
                                       labelText: '请输入密码',
                                       labelStyle: AppStyleThemeData.LABEL_TEXT_STYLE,
                                       //下面是重点
                                       suffixIcon: IconButton(
                                           icon: Icon(
                                             //根据passwordVisible状态显示不同的图标
                                             controller.pwVisible.value ? Icons.visibility : Icons.visibility_off,color: AppColors.disable_btn_bg_color,),
                                           onPressed: () {
                                             //更新状态控制密码显示或隐藏
                                             controller.pwVisible.value = ! controller.pwVisible.value;
                                           }),
                                       // 关键配置：自定义边框样式
                                       border: const UnderlineInputBorder(
                                         borderSide: BorderSide(color: AppColors.input_bottom_line_color), // 默认状态边框颜色
                                       ),
                                       enabledBorder: const UnderlineInputBorder(
                                         borderSide: BorderSide(color: AppColors.input_bottom_line_color), // 默认状态边框颜色
                                       ),
                                       focusedBorder: const UnderlineInputBorder(
                                         borderSide: BorderSide(color: Colors.blue), // 聚焦状态边框颜色
                                       ),
                                       errorBorder: const UnderlineInputBorder(
                                         borderSide: BorderSide(color: Colors.red), // 错误状态边框颜色
                                       ),
                                       focusedErrorBorder: const UnderlineInputBorder(
                                         borderSide: BorderSide(color: Colors.red), // 聚焦+错误状态边框颜色
                                       ),
                                       contentPadding: const EdgeInsets.fromLTRB(16, 20, 16, 14),
                                       // 可选：添加浮动标签动画间距调整
                                       floatingLabelAlignment: FloatingLabelAlignment.start

                                   ),
                                   validator: (v) {
                                     return CommonUtil.confirmValidatePassword(v!,controller.rePasswordCodeController.value.text);
                                   },
                                   onChanged: (v) {
                                     controller.onPwFieldChange(v);
                                   },

                                 )
                             ),
                             SizedBox(height: 10.h),
                             Container(
                                 child: TextFormField(
                                   keyboardType: TextInputType.text,
                                   controller: controller.rePasswordCodeController.value,
                                   obscureText: !controller.rePwVisible.value, //This will obscure text dynamically
                                   decoration: InputDecoration(
                                     //labelText: label,
                                       labelText: '请再次输入密码',
                                       labelStyle: AppStyleThemeData.LABEL_TEXT_STYLE,
                                       //下面是重点
                                       suffixIcon: IconButton(
                                           icon: Icon(
                                             //根据passwordVisible状态显示不同的图标
                                             controller.rePwVisible.value ? Icons.visibility : Icons.visibility_off,color: AppColors.disable_btn_bg_color,),
                                           onPressed: () {
                                             //更新状态控制密码显示或隐藏
                                             controller.rePwVisible.value = ! controller.rePwVisible.value;
                                           }),
                                       // 关键配置：自定义边框样式
                                       border: const UnderlineInputBorder(
                                         borderSide: BorderSide(color: AppColors.input_bottom_line_color), // 默认状态边框颜色
                                       ),
                                       enabledBorder: const UnderlineInputBorder(
                                         borderSide: BorderSide(color: AppColors.input_bottom_line_color), // 默认状态边框颜色
                                       ),
                                       focusedBorder: const UnderlineInputBorder(
                                         borderSide: BorderSide(color: Colors.blue), // 聚焦状态边框颜色
                                       ),
                                       errorBorder: const UnderlineInputBorder(
                                         borderSide: BorderSide(color: Colors.red), // 错误状态边框颜色
                                       ),
                                       focusedErrorBorder: const UnderlineInputBorder(
                                         borderSide: BorderSide(color: Colors.red), // 聚焦+错误状态边框颜色
                                       ),
                                       contentPadding: const EdgeInsets.fromLTRB(16, 20, 16, 14),
                                       // 可选：添加浮动标签动画间距调整
                                       floatingLabelAlignment: FloatingLabelAlignment.start

                                   ),
                                   validator: (v) {
                                     return CommonUtil.confirmValidatePassword(v!,controller.passwordCodeController.value.text);
                                   },
                                   onChanged: (v) {
                                     controller.onPwFieldChange(v);
                                   },

                                 )
                             ),
                             Container(
                               margin:  EdgeInsets.only(top: 30.h),
                               width: 594.w,
                               child: ElevatedButton(
                                   onPressed: controller.isSubmitBtnEnable.value == false ? null: () {
                                     if ((_formKey.currentState as FormState).validate()) {
                                          if (!controller.checkBoxChecked.value) {
                                            Tips.showToast('请勾选用户协议和隐私政策.');
                                            return;
                                          }

                                          if (CommonUtil.isEmail(controller.accountController.value.text)) { //邮箱.
                                            NetWorkManager.getInstance().requestBussiness(NetWorkManager.SEND_EMAIL_VERIFICATION_CODE, {
                                              'email': controller.accountController.value.text,
                                              'codemode': 1

                                            }, NetWorkManager.methodPOST, DataCallBack(
                                                businessCallBack: (v) {
                                                  Get.toNamed(Routes.VALIDATE_CODE, arguments: {
                                                    'account':controller.accountController.value.text,
                                                    'passwd': controller.passwordCodeController.value.text
                                                  });
                                                },
                                                errorCallBack: (v) {
                                                  Tips.showToast(v);
                                                }

                                            ),showLoading: true);


                                          } else {

                                            NetWorkManager.getInstance().requestBussiness(NetWorkManager.SEND_PHONE_VERIFICATION_CODE, {
                                              'phone': controller.accountController.value.text,
                                              'smsmode': 1

                                            }, NetWorkManager.methodPOST, DataCallBack(
                                                businessCallBack: (v) {
                                                  Get.toNamed(Routes.VALIDATE_CODE, arguments: {
                                                    'account':controller.accountController.value.text,
                                                    'passwd': controller.passwordCodeController.value.text
                                                  });
                                                  controller.accountController.value.text = '';
                                                  controller.passwordCodeController.value.text = '';
                                                  controller.rePasswordCodeController.value.text = '';
                                                  controller.setSuffixIconsVisible(false);
                                                },
                                                errorCallBack: (v) {
                                                  Tips.showToast(v);
                                                }

                                            ),showLoading: true);



                                          }







                                     }
                                   },
                                   child: const Text("账号注册")
                               ),
                             ),
                             SizedBox(height: 20.h),
                             Column(
                               crossAxisAlignment: CrossAxisAlignment.center,
                               children: [
                                 Row(
                                   mainAxisAlignment: MainAxisAlignment.center,
                                   children: [
                                     Obx(()=> MSHCheckbox(
                                       size: 16.w,
                                       value: controller.checkBoxChecked.value,
                                       colorConfig: MSHColorConfig.fromCheckedUncheckedDisabled(
                                         checkedColor: AppColors.Page_Title_Color,
                                       ),
                                       style: MSHCheckboxStyle.stroke,
                                       onChanged: (selected) {
                                         controller.checkBoxChecked.value = selected;
                                         controller.onCheckBoxChange();
                                       },
                                     )),
                                     SizedBox(width: 8.w,),
                                     RichText(
                                       text: TextSpan(
                                           children: [
                                             TextSpan(text: '我已阅读并同意', style: AppStyleThemeData.normal),
                                             TextSpan(text: '《用户协议》',style: AppStyleThemeData.linkStyle,recognizer: TapGestureRecognizer()..onTap=controller.onTapUserAgreementTask),
                                             TextSpan(text: '和', style: AppStyleThemeData.normal),
                                             TextSpan(text: '《隐私政策》',style: AppStyleThemeData.linkStyle,recognizer: TapGestureRecognizer()..onTap=controller.onTapPrivacyPolicyTask)

                                           ]
                                       ),
                                     )
                                     // Text('我已经阅读并同意 用户协议 隐私政策'),
                                   ],
                                 )
                               ],
                             )
                           ],
                         ),
                       ),
                     ))
                  ],
                ),
              )
            ],
          ),
        )
    );
  }


}
